package com.yanqu.road.logic.pb;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.entity.activity.operamask.game.OperaMaskSkill;
import com.yanqu.road.entity.activity.operamask.OperaMaskSceneRank;
import com.yanqu.road.pb.activity.OperaMaskProto;
import com.yanqu.road.pb.player.PlayerProto;

import java.util.ArrayList;
import java.util.List;

public class OperaMaskPb2 {

    public static OperaMaskSceneRank parseOperaMaskSceneRank(int activityId, int sceneId, OperaMaskProto.OperaMaskGetSceneRankTemp rankTemp) {
        OperaMaskSceneRank rank = new OperaMaskSceneRank();
        rank.setActivityId(activityId);
        rank.setSceneId(sceneId);
        rank.setUserId(rankTemp.getUserId());
        rank.setScore(Long.valueOf(rankTemp.getValue()));
        rank.setUseStep(rankTemp.getStep());
        List<OperaMaskSkill> skillList = new ArrayList<>();
        for (OperaMaskProto.OperaMaskSkillMsg skillMsg : rankTemp.getSkillListList()) {
            skillList.add(parseOperaMaskSkill(skillMsg));
        }
        rank.setSkillList(skillList);
        rank.setTime(System.currentTimeMillis());
        rank.setSingLevel(rankTemp.getSingLevel());
        rank.setSkillValue(rankTemp.getSkillValue());
        rank.setType(rankTemp.getType());
        return rank;
    }


    public static OperaMaskSkill parseOperaMaskSkill(OperaMaskProto.OperaMaskSkillMsg skillMsg) {
        return new OperaMaskSkill(skillMsg.getSkillId(), skillMsg.getLevel());
    }


    public static List<OperaMaskProto.OperaMaskSkillMsg> parseOperaMaskSkillMsgList(List<OperaMaskSkill> data) {
        List<OperaMaskProto.OperaMaskSkillMsg> list = new ArrayList<>();
        for (OperaMaskSkill skill : data) {
            list.add(OperaMaskProto.OperaMaskSkillMsg.newBuilder().setSkillId(skill.getSkillId()).setLevel(skill.getSkillLevel()).build());
        }
        return list;
    }

    public static List<OperaMaskProto.OperaMaskSkillMsg> parseOperaMaskSkillMsgList(String skillStr) {
        List<OperaMaskProto.OperaMaskSkillMsg> list = new ArrayList<>();
        List<OperaMaskSkill> skillList = JSONObject.parseObject(skillStr,  new TypeReference<List<OperaMaskSkill>>() {
        });
        for (OperaMaskSkill skill : skillList) {
            list.add(OperaMaskProto.OperaMaskSkillMsg.newBuilder().setSkillId(skill.getSkillId()).setLevel(skill.getSkillLevel()).build());
        }
        return list;
    }

    public static OperaMaskProto.OperaMaskGetSceneRankTemp.Builder parseOperaMaskGetSceneRankTemp(OperaMaskSceneRank data, PlayerProto.PlayerBaseTempMsg.Builder playerBaseTempMsg) {
        OperaMaskProto.OperaMaskGetSceneRankTemp.Builder builder = OperaMaskProto.OperaMaskGetSceneRankTemp.newBuilder();
        builder.setUserId(data.getUserId());
        builder.setPlayerBaseData(playerBaseTempMsg);
        builder.setValue(String.valueOf(data.getScore()));
        builder.setStep(data.getUseStep());
        builder.addAllSkillList(parseOperaMaskSkillMsgList(data.getSkillList()));
        builder.setSingLevel(data.getSingLevel());
        builder.setSkillValue(data.getSkillValue());
        builder.setType(data.getType());
        return builder;
    }


}
